package com.example.sp;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class BacGrou extends View {
	
	SoundPool sou;	
	int sID;
	
	float playrate = 1.f;
	float pitch = 1;
	
	ArrayList<Integer> gui;		
	ArrayList<Integer> streamList;
		
    int screenW;
    int screenH;
    
    float DeltaX, DeltaY;
	float dx, dy;
    
    int DrawX, DrawY;
    
    float ratioX = 1;
    float ratioY = 1;
    
    int StartYGuitar = 0;
    int Guitarwidth = 0;
    int instrumentWidth;
    int NumOfNote;
    int Octave;
    int uplimit;
    
    Bitmap bgr;
    Bitmap bmpchord;
    
    public int indexX = 0;
    public int indexY = 0;
    
    int movingBar = 12;
    int numOfString = 6;
    
    int CurrentString = 0;
    CImgButton b;

    public BacGrou(Context context , AttributeSet attrs) {
        super(context, attrs);	       
        bgr = BitmapFactory.decodeResource(getResources(),R.drawable.gtv1); //load a background
        bmpchord = BitmapFactory.decodeResource(getResources(),R.drawable.chord); //load chord
        
        sou = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    	
		gui = new ArrayList<Integer>();
		for(int i = 0; i < 50; i++)
		{
			gui.add(i);
		}
		
		ArrayList<String> Notename = new ArrayList<String>();
		for(int i = 0; i < 12; i++)
		{
			Notename.add("");
		}
		Notename.set(0, "c");
		Notename.set(1, "cs");
		Notename.set(2, "d");
		Notename.set(3, "ds");
		Notename.set(4, "e");
		Notename.set(5, "f");
		Notename.set(6, "fs");
		Notename.set(7, "g");
		Notename.set(8, "gs");
		Notename.set(9, "a");
		Notename.set(10, "as");
		Notename.set(11, "b");
		
		int nn = 0;
		for(int i = 0; i < 49; i++)
		{
			String p = "ng" + String.valueOf(i+1) + Notename.get(nn);
			int temp = context.getResources().getIdentifier(p, "raw", "com.example.sp");			
			gui.set(i, sou.load(context, temp, 1));
			
			nn++;
			if(nn > 11) 
				nn = 0;
		}		
		streamList = new ArrayList<Integer>();	
		
    }
    
    public int getIndex()
    {    	
    	indexX = (int)((DeltaX - DrawX) / instrumentWidth * (float)NumOfNote);
		indexY = numOfString - 1 - (int)((DeltaY - DrawY - StartYGuitar) / (Guitarwidth) * (float)numOfString);
    	int NoteIndex = -1;
		switch(indexY)
		{
			case 0:
			case 1:
			case 2:
			case 3:			
			{
				NoteIndex = indexX + (indexY + 1) * 5 - 1;
				break;
			}
			case 4:
			case 5:
			{
				NoteIndex = indexX + (indexY + 1) * 5 - 2;
				break;
			}			
			
		}
		
    	return NoteIndex;
    }
    
    @Override
	public boolean onTouchEvent(MotionEvent event)  {
		// TODO Auto-generated method stub
		if (event.getAction()==MotionEvent.ACTION_DOWN)		
		{	
			dx = DeltaX = event.getX();
			dy = DeltaY = event.getY() ;			
			
			if(b.OnClick((int)DeltaX, (int)DeltaY))
			{
				Chord(0);
			}
			
			if(dy < StartYGuitar)
				return true;
									
			playrate = 1.f;
			if(getIndex() > -1)
			{		
				CurrentString =indexY; 					
				sID = sou.play(gui.get(getIndex()), 1, 1, 1, 0, 1);				
				streamList.add(sID);
			}			
			return true;
		}
		if (event.getAction()==MotionEvent.ACTION_MOVE)					
		{		
								
			DeltaX = event.getX();
			DeltaY = event.getY();	
			if(DeltaY < movingBar)
				if(DrawX <= 0 && DrawX >= uplimit)
				{
					DrawX += DeltaX - dx;
					if(DrawX > 0)
						DrawX = 0;
					if(DrawX < uplimit)
						DrawX = uplimit;
				}
			
			if(getIndex() > -1)
			if(CurrentString != indexY)
			{				
				CurrentString = indexY; 
				sID = sou.play(gui.get(getIndex()), 1, 1, 1, 0, 1);
				streamList.add(sID);
			}
			
			//playrate += (DeltaX - dx)/((float)getWidth()) * 11/12 + (DeltaY - dy)/(float)getHeight()/10;
			playrate += (DeltaX - dx)/((float)getWidth()) * 10/12;
			sou.setRate(sID, playrate);
			
			dx = DeltaX = event.getX();
			dy = DeltaY = event.getY();
			
			b.OnHover((int)DeltaX, (int)DeltaY);
			
            return true;
		}	
		if (event.getAction()==MotionEvent.ACTION_UP)
		{			
			b.OnOut();
			//sou.stop(sID);
			//sID = sou.play(gui.get(8), 1, 1, 1, 0, playrate);
			return true;
		}
		return false;	
		
	}
    
    void Chord(int chord)
    {
    	
    }
    
    void InitParameter()
    {
    	DrawX = 0;
		DrawY = 0;
		
		Octave = 2;
		NumOfNote = 24; // From E to D#				    
		numOfString = 6;
		
		playrate = 1.f;
		pitch = 1;
        
		instrumentWidth = screenW * Octave;
		uplimit = -getWidth() * (Octave - 1);
		
		ratioX = (float)screenW * 2 / (float)bgr.getWidth();
		ratioY = (float)screenH / (float)bgr.getHeight();
		
		movingBar = (int)(80  * ratioY);
		
		StartYGuitar = movingBar + (int)(80 *ratioY);
		Guitarwidth = (int)(600 * ratioY);  
		
		b = new CImgButton(this, R.drawable.testbtn, ratioX, 0, 0, 3);
	
    }
    
    
    
    @Override
    public void onSizeChanged (int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        screenW = w;
        screenH = h;
        
		InitParameter();
		
        bgr = Bitmap.createScaledBitmap(bgr, instrumentWidth, h, true); //Resize background to fit the screen.
        bmpchord = Bitmap.createScaledBitmap(bmpchord, (int)(bmpchord.getWidth() * ratioX), screenH, true);        
    }
    
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);        
        //Draw background.     
        canvas.drawBitmap(bgr, DrawX , DrawY, null);
        canvas.drawBitmap(bmpchord, screenW - bmpchord.getWidth(), 0, null);
        b.OnDraw(canvas);
        //Call the next frame.
        invalidate();
    }
    
    
    public void onDestroy()
    {
    	sou.release();
    }
    
}